/*
   Copyright (C) 2007 Yen-Ju Chen <yjchenx gmail>

   This application is free software; you can redistribute it and/or 
   modify it under the terms of the MIT license. See COPYING.

*/

#import "KRItemTableViewController.h"

@implementation KRItemTableViewController
/* Private */
- (void) _rebuildTableView
{
	/* Rebuild columns of item view based on currentFactory */
	if (_currentFactory)
	{
		/* Remove all table columns first */
		NSTableColumn *column = nil;
		NSEnumerator *e = [[_tableView tableColumns] objectEnumerator];
		while ((column = [e nextObject]))
		{
			[_tableView removeTableColumn: column];
		}
   
		e = [[_currentFactory propertiesToDisplay] objectEnumerator];
		NSString *iden = nil;
		while ((iden = [e nextObject]))
		{
			column = [[NSTableColumn alloc] initWithIdentifier: iden];
			[column setEditable: NO];
			NSLocalizedStringFromTableInBundle(iden, nil, [NSBundle bundleForClass: [_currentFactory class]], nil);
			[[column headerCell] setStringValue: [[NSBundle bundleForClass: [_currentFactory class]] localizedStringForKey: iden value: nil table: nil]];
			[_tableView addTableColumn: column];
			DESTROY(column);
		}
	}
}

/* End of private */
- (NSView *) itemView
{
	return _scrollView;
}

- (void) refresh
{
	/* Rebuild items */
	if (_predicate)
	{
		ASSIGN(_items, [_group objectsMatchingPredicate: _predicate]);
	}
	else
	{
		ASSIGN(_items, AUTORELEASE([[NSMutableArray alloc] init]));
		[_items addObjectsFromArray: [_group objects]];
		[_items addObjectsFromArray: [_group subgroups]];
	}
	[_tableView reloadData];
}

- (void) setGroup: (OKGroup *) group
{
	ASSIGN(_group, group);
	_currentFactory = [_dataSource factoryForGroup: _group];
	[self _rebuildTableView];
}

- (void) setPredicate: (NSPredicate *) predicate
{
	ASSIGN(_predicate, predicate);
	[self refresh];
}

- (void) setDataSource: (NSObject <KRDataSourceProtocol> *) dataSource
{
	ASSIGN(_dataSource, dataSource);
}

/* Table View */
- (void) tableViewSelectionDidChange: (NSNotification *) not
{
	int index = [_tableView selectedRow];
	OKObject *object = nil;
	if ((index == NSNotFound) || (index == -1))
	{
		/* No selection */
	}
	else
	{
		/* Give it a chance to fill the subgroups */
		object = [_items objectAtIndex: index];
	}
	
	if ([_currentFactory respondsToSelector: @selector(didSelectObject:)])
	{
		[_currentFactory didSelectObject: object];
	}
}

- (int) numberOfRowsInTableView: (NSTableView *) tv
{
	if (_items)
	{
		return [_items count];
	}
	return 0;
}

- (id) tableView: (NSTableView *) tv
       objectValueForTableColumn: (NSTableColumn *) tc row: (int) index
{
	if (_items)
	{
		OKObject *object = [_items objectAtIndex: index];
		return [object valueForProperty: [tc identifier]];
	}
	return nil;
}

/* NSObject */
- (id) init
{
	self = [super init];
	NSRect rect = NSMakeRect(0, 0, 200, 200);
	_scrollView = [[NSScrollView alloc] initWithFrame: rect];
	_tableView = [[NSTableView alloc] initWithFrame: rect];
	NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier: @"First"];
	[_tableView addTableColumn: column];
	[_tableView setDataSource: self];
	[_tableView setDelegate: self];
	[_tableView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
	[_scrollView setDocumentView: _tableView];
	[_scrollView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
	[_scrollView setHasHorizontalScroller: YES];
	[_scrollView setHasVerticalScroller: YES];
	DESTROY(column);
	return self;
}

- (void) dealloc
{
	DESTROY(_scrollView);
	DESTROY(_tableView);
	DESTROY(_group);
	DESTROY(_predicate);
	[super dealloc];
}
@end

